$(function() {

	var baseUrl = $("#project_base_url").attr("content");
	$("#bankType").val(33);
	$("#form").validate({
		rules : {
			"cardholderName" : {
				required : true,
				chinese : true
			},
			"cardholderNo" : {
				required : true,
				bankCard : true,
				minlength : 14,
				remote : baseUrl + "/signed/bank/cardflagValidate"
			},
			"idCard" : {
				required : true,
				idCard : true,
				remote : baseUrl + "/signed/bank/idCardValidate"
			},
			"mobile" : {
				required : true,
				tel : true
			},
			"captcha" : {
				required : true,
				cqrcb : true
			},
			"checkbox" : {
				required : true
			},
			"bankBranchNo" : {
				required : true
			}
		},
		messages : {
			"cardholderName" : {
				required : "请输入持卡人姓名",
				chinese : "请正确输入持卡人姓名"
			},
			"cardholderNo" : {
				required : "输入银行卡号",
				remote : "该银行暂不支持",
				bankCard : "请正确输入银行卡号码",
				minlength : "请输入正确的银行卡号码",
			},
			"idCard" : {
				required : "请输入身份证号码",
				idCard : "请正确输入身份证号码",
				remote : "该身份证号码已经绑定"
			},
			"mobile" : {
				required : "请输入银行预留手机"
			},
			"checkbox" : {
				required : "请先阅读注册协议"
			},
			"bankBranchNo" : {
				required : "请选择开户行"
			}
		}
	});
	$("#cardholderNo").keyup(function() {
		var value = $("#cardholderNo").val();
		var score = /^[0-9]{6,20}$/;
		if (score.test(value)) {
			var begCard = value.substring(0, 6);
			if (nextCard != begCard) {
				nextCard = begCard;
				$.post(baseUrl + "/signed/bank/cardflag", {
					"cardholderNo" : begCard
				}, function(data) {
					if (data && data.state) {
						$("#bankType").val(data.bankCode);
						var bankName = $("a[code=" + data.bankCode + "]").attr("name");
						$("#bankName").val(bankName);
					}
					var value = $("#bankType").val();
					if (value == "33") {
						$("#div-cqrcb").hide();
						$("#bankBranchNo").val("-1");
					} else {
						$("#bankBranchNo").val("");
						$("#div-cqrcb").show();
					}
				}).error(function() {
					var value = $("#bankType").val();
					if (value == "33") {
						$("#div-cqrcb").hide();
						$("#bankBranchNo").val("-1");
					} else {
						$("#bankBranchNo").val("");
						$("#div-cqrcb").show();
					}
				});
			}
		}
	});
	var isRun = false;
	$("#submit").click(function() {
		console.log(isRun);
		if (!isRun) {
			isRun = true;
			console.log($("#form").valid());
			if ($("#form").valid()) {
				showLoading("请稍候，正在提交绑卡信息…")
				var bankType = $("#bankType").val();
				var bankName = $("#bankName").val();
				$.post(baseUrl + "/signed/bank/bind",{
					"p2p_user_id":$("#p2p_user_id").val(),
					"cardholderNo" : $("#cardholderNo").val(),
					"cardholderName" : $("#cardholderName").val(),
					"bankType" : bankType,
					"bankName" : bankName,
					"idCard" : $("#idCard").val(),
					"mobile" : $("#mobile").val(),
					"captcha" : $("#captcha").val(),
					"seqCode" : $("#seqCode").val(),
					"province" : $("#province").val(),
					"city" : $("#city").val(),
					"bankBranchNo" : $("#bankBranchNo").val(),
					"bankBranchName" : $("#bankBranchName").val()
				},
				function(data) {
					console.log(data);
					isRun = false;
					hideLoading();
					if (data.state) {
						successTimeUrl("绑卡成功", 5, data.callBack);
					} else {
						errorTime(data.info, 5);
					}
				}).error(function() {
					isRun = false;
					hideLoading();
					errorTime("连接服务器失败，请稍候重试", 5);
				});
			} else {
				isRun = false;
			}
		}
	});
	var dataMap = {};
	var nextCard = "";
	$.post(baseUrl + "/signed/bank/city", function(data) {
		dataMap = data;
		var html = "";
		for ( var key in dataMap) {
			var row = dataMap[key];
			if (row.region_name == "重庆市") {
				html += "<option selected=\"selected\" value=\"" + row["id"]
						+ "\">" + row["region_name"] + "</option>";
				child(key);
			} else {
				html += "<option value=\"" + row["id"] + "\">"
						+ row["region_name"] + "</option>";
			}
		}
		$("#province").html(html);
	});

	$("#province").change(function() {
		child($("#province").val());
	});
	function child(key) {
		var html = "";
		$.each(dataMap[key]["child"], function(index, row) {
			if (index == 0) {
				html += "<option selected=\"selected\" value=\"" + row["id"]
						+ "\">" + row["region_name"] + "</option>";
			} else {
				html += "<option value=\"" + row["id"] + "\">"
						+ row["region_name"] + "</option>";
			}
		});
		$("#city").html(html);
	}
	$("#sendRegisterCode").click(function() {
		if (!isRunSendSms) {
			isRunSendSms = true;
			var score = /^1[0-9]{10}$/;
			if (score.test($("#mobile").val())) {
				$("#sendRegisterCode").html("正在发送...");
				$.post(baseUrl + "/signed/bank/captcha", {
					"mobile" : $("#mobile").val()
				}, function(data) {
					console.log(data);
					if (data.state) {
						remainTime();
						$("#sendRegisterCode").addClass("obtained");
						$("#seqCode").val(data.serialNo);
					} else {
						isRunSendSms = false;
						$("#seqCode").val("");
						$("#sendRegisterCode").html("获取验证码");
						errorTime(data.info, 3);
					}
				}).error(function() {
					isRunSendSms = false;
					$("#sendRegisterCode").html("获取验证码");
					errorTime("获取验证码失败,请稍后重试", 3);
				});
			} else {
				$("#form").valid();
			}
		}
	});
	$("#bankBranchName").click(function() {
		$("#bankBranch-pop").fadeIn("fast");
		var bankType = $("#bankType").val();
		var bankName = $("a[code=" + bankType + "]").attr("name");
		$("#bankBranchSearch").val(bankName);
	});
	$(".close").click(function() {
		$("#bankBranch-pop").fadeOut("fast");
		var bankType = $("#bankType").val();
		var bankName = $("a[code=" + bankType + "]").attr("name");
		if ($("#bankBranchSearch").val() == bankName) {
			$("#bankNameList").html("");
		}
	});
	$(".tanc-bg").click(function() {
		$("#bankBranch-pop").fadeOut("fast");
		var bankType = $("#bankType").val();
		var bankName = $("a[code=" + bankType + "]").attr("name");
		if ($("#bankBranchSearch").val() == bankName) {
			$("#bankNameList").html("");
		}
	})

	$("#bankNameList").html("");
	$("#bankBranchSearchBtn").click(function() {
		var value = $("#bankBranchSearch").val();
		var bankType = $("#bankType").val();
		var bankName = $("a[code=" + bankType + "]").attr("name");
		if (value.indexOf(bankName) < 0) {
			errorTime("请输入正确的银行名称。例如：" + bankName,3);
		} else {
			if (!isRunSearch) {
				isRunSearch = true;
				showLoading("请稍候,正在加载开户支行信息.....");
				$.post(baseUrl+ "/signed/bank/searchBank",{
					"bankName" : $("#bankBranchSearch").val()
				},
				function(data) {
					isRunSearch = false;
					console.log(data);
					hideLoading();
					if (data.state) {
						if (data.data.totalNum > 0) {
							var html = "";
							$.each(data.data.list,function(index,row) {
								html += '<tr><td>'
										+ row.bankName
										+ '</td><td class="xz"><a href="javascript:void(0)" bankNo="'
										+ row.bankNo
										+ '" bankName="'
										+ row.bankName
										+ '">选择</a></td></tr>';
							});
							$("#bankNameList").html(html);
							$("#bankNameList a").click(function() {
								var code = $(this).attr("bankNo");
								var name = $(this).attr("bankName");
								$("#bankBranchName").val(name);
								$("#bankBranchNo").val(code);
								// console.log($(dom).attr("bankNo")+":"+$(dom).html());
								$(".tanc4").fadeOut("fast");
								var bankType = $("#bankType").val();
								var bankName = $("#bankType option[value="+ bankType+ "]").html();
								if ($("#bankBranchSearch").val() == bankName) {
									$("#bankNameList").html("");
								}
							});
						} else {
							$("#bankNameList").html("<tr><td colspan=\"2\"> 暂无数据</td></tr>");
						}
					} else {
						$("#bankNameList").html("<tr><td colspan=\"2\">加载失败,请重试</td></tr>");
					}
				}).error(function() {
					isRunSearch = false;
					hideLoading();
					$("#bankNameList").html("<tr><td colspan=\"2\">连接服务器失败,请稍后重试</td></tr>");
				});
			}
		}
	});

});
var isRunSearch = false;
var isRun = false;
var isRunSendSms =false;
var i = 180;
function remainTime() {
	if (i == 0) {
		i = 180;
		$("#sendRegisterCode").html("获取验证码");
		$("#sendRegisterCode").removeClass("obtained");
		isRunSendSms = false;
	} else {
		$("#sendRegisterCode").html((i--) + "秒后获取");
		setTimeout("remainTime()", 1000);
	}
}